package com.xdog.web3.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xdog.web3.config.Web3ConfigService;
import com.xdog.web3.domain.indexprice.TokenHistoricalPriceQuery;
import com.xdog.web3.domain.indexprice.TokenIndexPriceRequest;
import com.xdog.web3.service.IIndexPriceService;
import com.xdog.web3.utils.Web3ApiUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 综合价格服务实现类
 */
@Slf4j
@Service
public class IndexPriceServiceImpl implements IIndexPriceService {

    @Resource
    private Web3ConfigService web3ConfigService;

    @Override
    public JSONObject getTokenIndexPrice(List<TokenIndexPriceRequest> requests) {
        String requestPath = "/api/v6/dex/index/current-price";
        return Web3ApiUtil.post(requestPath, requests);
    }

    @Override
    public JSONObject getTokenHistoricalPrice(TokenHistoricalPriceQuery query) {
        String requestPath = "/api/v6/dex/index/historical-price";

        Map<String, String> params = new HashMap<>();
        params.put("chainIndex", query.getChainIndex());

        if (query.getTokenContractAddress() != null) {
            params.put("tokenContractAddress", query.getTokenContractAddress());
        }

        if (query.getLimit() != null) {
            params.put("limit", query.getLimit());
        }

        if (query.getCursor() != null) {
            params.put("cursor", query.getCursor());
        }

        if (query.getBegin() != null) {
            params.put("begin", query.getBegin());
        }

        if (query.getEnd() != null) {
            params.put("end", query.getEnd());
        }

        if (query.getPeriod() != null) {
            params.put("period", query.getPeriod());
        }

        return Web3ApiUtil.get(requestPath, params);
    }
}
